﻿/*****************************************************************
// Copyright (C)
// All rights reserved.
// 
// Author:       WYQ
// Create Date:  2012-8-18 15:16:02
// Usage: 持久化数据库仓库
*****************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Portal.Ef.Repository
{

   
    public class  OrderRepository : Repository<Order, Order, TbOrder>, IOrderRepository<Order>
    {
        public override Order CreateEntity()
        {
            return new Order { };
        }
        public override IQueryable<Order> Get()
        {
            return MapQueryable(Entities.TbOrder);
        }
        public override long Count()
        {
            return MapQueryable(Entities.TbOrder).Count();
        }
        public override IQueryable<Order> Get(Expression<Func<Order, bool>> expression)
        {
            return MapQueryable(Entities.TbOrder).Where(expression);
        }
        public override IQueryable<Order> GetSort(string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbOrder), sort, order);
        }
        public override IQueryable<Order> GetSort(Expression<Func<Order, bool>> expression, string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(expression, "查询条件");
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbOrder).Where(expression), sort, order);
        }
        public override void Add(Order t)
        {
            Entities.AddToTbOrder(ReverseMapModel(t));
        }
        public override void Update(Order t)
        {
            throw new NotImplementedException();
        }
        public override void AddOrUpdate(Order t)
        {
            if (Entities.TbOrder.Any(o => o.CoOrderId == t.Id))
            {

            }
            else
            {
                Entities.AddToTbOrder(ReverseMapModel(t));
            }
        }
        public override void Delete(Order t)
        {
            Entities.TbOrder.Where(o => o.CoOrderId == t.Id).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Delete(Expression<Func<Order, bool>> expression)
        {
            MapQueryable(Entities.TbOrder).Where(expression).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Save()
        {
            Entities.SaveChanges();
        }
       
        public JqEasyuiDataGrid<Order> JqEasyuiDataGrid(string code, string name, string categoryId, string sort = "CreateTime", string order = "Desc", int page = 10, int index = 1)
        {
            return new JqEasyuiDataGrid<Order>();
        }
      
        public override sealed IQueryable<Order> MapQueryable(IQueryable<TbOrder> queryable)
        {
            return queryable.Select(o => new Order 
            {
         
	           Id=o.CoOrderId,
	           Name = o.CoName 
,
	           CreateTime = o.CoCreateTime??DateTime.Now 
,
	           IsDelete = o.CoIsDelete??false 
,
	           Version = o.CoVersion??0 
,
	           IsClosed = o.CoIsClosed??false 
,
	           Culture = o.CoCulture 
,
	           Code = o.CoCode 
,
	           CategoryId = o.CoCategoryId 
,
	           Sort = o.CoSort??0 
,
	           Email = o.CoEmail 
,
	           Qq = o.CoQq 
,
	           Telnet = o.CoTelnet 
,
	           Mobil = o.CoMobil 
,
	           UserId = o.CoUserId 
,
	           UserName = o.CoUserName 
,
	           Note = o.CoNote 
,
	           Addrees = o.CoAddrees 
,
	           TotalAmount = o.CoTotalAmount??0 
            
            });
        }
      
        public override sealed IQueryable<TbOrder> ReverseMapQueryable(IQueryable<Order> queryable)
        {
            return queryable.Select(o => new TbOrder 
            { 
        
	           CoOrderId=o.Id,
	           CoName = o.Name
,
	           CoCreateTime = o.CreateTime
,
	           CoIsDelete = o.IsDelete
,
	           CoVersion = o.Version
,
	           CoIsClosed = o.IsClosed
,
	           CoCulture = o.Culture
,
	           CoCode = o.Code
,
	           CoCategoryId = o.CategoryId
,
	           CoSort = o.Sort
,
	           CoEmail = o.Email
,
	           CoQq = o.Qq
,
	           CoTelnet = o.Telnet
,
	           CoMobil = o.Mobil
,
	           CoUserId = o.UserId
,
	           CoUserName = o.UserName
,
	           CoNote = o.Note
,
	           CoAddrees = o.Addrees
,
	           CoTotalAmount = o.TotalAmount
  
            
            });
        }
      
        public override sealed Order MapModel(TbOrder model)
        {
            return new Order {
	           Id=model.CoOrderId,
	           Name = model.CoName
,
	           CreateTime = model.CoCreateTime??DateTime.Now
,
	           IsDelete = model.CoIsDelete??false
,
	           Version = model.CoVersion??0
,
	           IsClosed = model.CoIsClosed??false
,
	           Culture = model.CoCulture
,
	           Code = model.CoCode
,
	           CategoryId = model.CoCategoryId
,
	           Sort = model.CoSort??0
,
	           Email = model.CoEmail
,
	           Qq = model.CoQq
,
	           Telnet = model.CoTelnet
,
	           Mobil = model.CoMobil
,
	           UserId = model.CoUserId
,
	           UserName = model.CoUserName
,
	           Note = model.CoNote
,
	           Addrees = model.CoAddrees
,
	           TotalAmount = model.CoTotalAmount??0
            };
        }
        
        public override sealed TbOrder ReverseMapModel(Order model)
        {
            return new TbOrder { 
	           CoOrderId=model.Id,
	           CoName = model.Name
	        
,
	           CoCreateTime = model.CreateTime
	        
,
	           CoIsDelete = model.IsDelete
	        
,
	           CoVersion = model.Version
	        
,
	           CoIsClosed = model.IsClosed
	        
,
	           CoCulture = model.Culture
	        
,
	           CoCode = model.Code
	        
,
	           CoCategoryId = model.CategoryId
	        
,
	           CoSort = model.Sort
	        
,
	           CoEmail = model.Email
	        
,
	           CoQq = model.Qq
	        
,
	           CoTelnet = model.Telnet
	        
,
	           CoMobil = model.Mobil
	        
,
	           CoUserId = model.UserId
	        
,
	           CoUserName = model.UserName
	        
,
	           CoNote = model.Note
	        
,
	           CoAddrees = model.Addrees
	        
,
	           CoTotalAmount = model.TotalAmount
	        
            
            
            };
        }


    }
}



